from sys import path as path2
from time import sleep, localtime, strftime
from threading import Thread, active_count

import requests
from lxml import html
from rich import print as rprint

path2.append('c:/users/tian/desktop')
from Service import service, ua
from service import title2tag, req


def req82bt(link):
    while True:
        try:
            r = requests.post('https://www.82bt.com/downs-m.php', data={'code': link.split('=')[-1]}, timeout=3,
                              headers={'User-Agent': ua.ua, 'referer': link})
            break
        except Exception as e:
            rprint('请求 82bt 报错：', e)
            sleep(0.5)
    return r.text

class Gcbt:
    def __init__(self):
        service.path = 'd:/data/gcbt.db'
        self.newNum = 0     # 新增记录的数量
    
    def prepare(self, startIndex, endIndex):
        for i in range(startIndex, endIndex):
            indexUrl = f'https://gcbt2.xyz/page/{i}'
            while True:
                try:
                    # node_h2 = self.R.req(indexUrl)
                    node_h2 = req.req3(indexUrl).xpath('//h2/a')
                    break
                except Exception as e:
                    rprint('[red]索引页解析出错')
                    rprint('报错信息：', e)
                    sleep(2)
            for h2 in node_h2:
                Thread(target=self.get, args=(h2, i)).start()
                sleep(0.08)
                while True:
                    if active_count() < 11:
                        break
        while True:         # 等待爬取线程都结束
            if active_count() == 1:
                break
            else:
                sleep(0.2)
        rprint(f'Gcbt 爬取完毕，本次新增 {self.newNum} 条数据')

    def get(self, h2, i):
        _title = h2.xpath('./@title')[0]
        title = ''.join(_title.split()).lower()      # 删除空格或多个连续空格
        tag = title2tag.aboutTitle(service.path, title, i, 3)
        if not tag:
            return
        url = h2.xpath('./@href')[0]
        _html = req.req3(url)
        postDate = _html.xpath('//time/@datetime')[0].split('T')[0]
        _magnet = _html.xpath('//a[starts-with(@href, "magnet:?xt")]/@href')
        if _magnet != []:
            _magnet = _magnet[0]
            link = 'no'
        else:
            div  = _html.xpath('//div[@class="entry-content u-text-format u-clearfix"]')
            for subnode in div:
                sl = subnode.xpath('.//text()')
                if not sl:      # 跳过为空的
                    continue
                for s in sl:
                    if 'http' in s:
                        link = str(s)       # 不然，s 的类型是：lxml.etree._ElementUnicodeResult
            if not ('link' in locals()):    # 极个别情况，文中无链接
                print('未找到链接')
                with open('d:/exception_gcbt.txt', 'a', encoding='utf-8') as f:
                    f.write(f'{url}\n')        # 将网址写入本地，人工确定
                return
            if 'ww1.k00ppc' in link or 'ww1.picd231' in link or 'ww1.91dfjh' in link or 'picd232' in link or 'picdn' in link:    # 关于 bi2303–picdn.cc，正确的网址，中间有两个 -
                if 'hash=' in link:
                    _magnet = 'magnet:?xt=urn:btih:' + link.split('hash=')[-1]
                else:
                    _magnet = 'magnet:?xt=urn:btih:' + link.split('/')[-1]
            elif 'rmdown' in link:
                _magnet = 'magnet:?xt=urn:btih:' + link.split('hash=')[-1][3:]
            elif 'www.82bt.com' in link:
                if 'hash' in link:
                    _magnet = 'magnet:?xt=urn:btih:' + link.split('hash=')[-1][3:]
                else:
                    agnet = req82bt(link)
                    if agnet.startswith('<br />'):      # 网站不能正确显示磁力链接
                        return                          # 不记录此视频
                    _magnet = 'magnet:?xt=urn:btih:' + agnet
            else:
                rprint(f'[red]未知链接：{url} | {link}')
                _magnet = '未知'
        
        magnet = _magnet.split("&")[0].lower()
        recorddate = strftime('%Y-%m-%d %H:%M', localtime())
        status = 0
        # rprint(tag, title, url, link, magnet, postDate)
        service.exec('insert into guochan(tag, title, url, link, magnet, postdate, recorddate, status) values (?, ? ,? , ?, ?, ?, ?, ?)',
                    (tag, title, url, link, magnet, postDate, recorddate, status))
        self.newNum += 1
        rprint(f'{self.newNum} ({i})')